package service

import (
	"ce-cloud-service/internal/define"
	"ce-cloud-service/internal/proto/base/user"
	"ce-cloud-service/pkg/tool"
	"github.com/gin-gonic/gin"
	"go-micro.dev/v4/client"
	"time"
)

type UserService struct {
	Svc user.UserService
}

func newUserService(cli client.Client) *UserService {
	obj := new(UserService)
	obj.Svc = user.NewUserService(define.ServiceNameBaseUser, cli)
	return obj
}

func (s *UserService) Register(ctx *gin.Context, in *user.UserRegisterRequest) (out *user.UserRegisterResponse, err error) {
	return s.Svc.UserRegister(ctx, in)
}

func (s *UserService) Login(ctx *gin.Context, in *user.UserLoginRequest) (out *user.UserLoginResponse, sign string, err error) {
	rsp, err := s.Svc.UserLogin(ctx, in)
	if err != nil {
		return nil, "", err
	}
	generateSign, err := tool.Sign{}.GenerateSign(tool.Sign{Data: tool.SignData{
		Uid:       rsp.UserId,
		Account:   in.Account,
		LoginTime: time.Now().Unix(),
	}})
	if err != nil {
		return nil, "", err
	}
	return rsp, generateSign, nil
}
